
//
// 110514 - AH - (Garage) Created
// 110625a- AH - (Fotballpuben *) Moved method to check for Pit content to Pit.
// 110625b- AH - (Fotballpuben *) Missing java doc.
// 110719 - AH - (Garage) addNoteRequestEventListener(NoteRequestEventListener),
//               removeNoteRequestEventListener(NoteRequestEventListener) and
//               fireNoteRequest() method for implementation to use.
// 110722 - AH - (Garage) Removed code of 110719, not needed.
// 110726 - AH - isPureNote(String).
// 110805 - AH - setNote(String).
//

package org.soekrat.gui.swing.chat.objectconversation;

import javax.swing.JPanel;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.pit.Content;
import org.soekrat.engine.pit.Node;
import org.soekrat.engine.pit.Pit;
import org.soekrat.engine.wheel.Wheel;
import org.soekrat.gui.swing.chat.objectconversation.note.NoteDialog;
import org.soekrat.gui.swing.chat.objectconversation.note.NoteUtil;
import org.soekrat.gui.swing.html.BrowserAccess;

/**
 * <p>
 *   Base class for
 *   {@link JPanel}s that shows retrieved information from the 
 *   {@link Wheel}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public abstract class ConversationComponent extends JPanel
{ 
  protected final String m_ontology;
  
  protected final Soekrat2Engine m_engine;
  
  private final BrowserAccess m_ba=new BrowserAccess();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine {@link Soekrat2Engine}.
   */
  protected ConversationComponent(Soekrat2Engine engine)
  {
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    
    m_ontology=null;
    m_engine=engine;
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param engine   {@link Soekrat2Engine}.
   * @param ontology Name on ontology to show content for.
   */
  protected ConversationComponent(Soekrat2Engine engine, String ontology)
  {
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    if (ontology==null)
    {
      throw new NullPointerException("ontology");
    }
    
    m_ontology=ontology;
    m_engine=engine;
  }
  
  /**
   * <p>
   *   Gets
   *   {@link Soekrat2Engine}.
   * </p>
   * @return {@code SoekratEngine}.
   */
  public Soekrat2Engine getSoekratEngine(){ return m_engine; }
  
  /**
   * <p>
   *   Gets name on ontology this should be aware of when searching the
   *   {@link Pit}.
   * </p>
   * @return Name or {@code null} if search the
   *         {@link Pit} for now specific ontology.
   */
  public String getOntology(){ return m_ontology; }
  
  /**
   * <p>
   *   If any content found in 
   *   {@link Pit} for node named {@code name} show it.
   * </p>
   * @param name Node name.
   */
  protected final void showContent(String name)
  {
    Pit pit=m_engine.getPit();
    Node node=pit.getNode(name);
    if (node!=null)
    {
      Content[] content=(m_ontology==null) ? node.getContent() :
                                             node.getContent(m_ontology);
      for (Content curr : content) 
      {
        if (!curr.isNote())
        {
          String text=curr.getText();
          m_ba.open(text);
        }
      }
    }
  }
  
  /**
   * <p>
   *   Tells if in this context name has purely note content.
   * </p>
   * @param name Node name.
   * @return {@code true} if is so, {@code false} if is not so.
   */
  protected final boolean isPureNote(String name)
  {
    Pit pit=m_engine.getPit();
    Node node=pit.getNode(name);
    
    if (node!=null)
    {
      Content[] content=(m_ontology==null) ? node.getContent() :
                                             node.getContent(m_ontology);
      for (Content curr : content)
      {
        if (!curr.isNote()) return false;
      }
    }
    
    return true;
  }
  
  private NoteDialog m_noteDialog=new NoteDialog();
  
  /**
   * <p>
   *   Request note from end user.
   * </p>
   * @param txt Existing note text, {@code null} if there is non.
   * @return Note text end user or {@code null} if did not make note.
   */
  public String requestNote(String txt)
  {
    return m_noteDialog.showDialog(txt); 
  }
  
  /**
   * <p>
   *   Requests not from end user, view note to end user and update node with
   *   any note content change this causes.
   * </p>
   * <p>
   *   Note considered is in context of ontology named
   *   {@link #getOntology()}.
   * </p>
   * @param name Name on node.
   * @return {@code true} if after this node has note else returns 
   *         {@code false}.
   */
  public boolean setNote(String name)
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    
    Pit pit=m_engine.getPit();
    
    String ontology=getOntology();
    
    Node node=pit.getNode(name);
    Content c=NoteUtil.getNoteContent(node, getOntology());
    
    String txt=(c==null) ? null : c.getText();
    txt=requestNote(txt);
    if (txt!=null)
    {
      if (c==null)
      {
        c=new Content(txt, true);
        c.addOntology(ontology);
        node.addContent(c);
      }
      else
      {
        c.setText(txt);        
      }
      
      return true;
    }
    else
    {
      if (c!=null) node.removeContent(c);
      
      return false;
    }
  }
  
}
